package com.yphone.tools;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;

import com.google.gson.reflect.TypeToken;
import com.iiordanov.bVNC.App;
import com.iiordanov.bVNC.ConnectionBean;
import com.iiordanov.bVNC.RemoteCanvasActivity;
import com.iiordanov.bVNC.Utils;
import com.iiordanov.bVNC.event.bVNCEvent;
import com.yphone.base.utils.SharepreferenceUtils;
import com.yphone.net.HttpConstants;
import com.yphone.net.IUIDataListener;
import com.yphone.net.bean.BaseResultWrapper;
import com.yphone.net.helper.ActivityHttpHelper;
import com.yphone.ui.login.manager.LoginManager;
import com.yphone.ui.login.respone.UserInfo;
import com.yphone.ui.ysj.bean.RemoteConnectBean;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RemoteUtils {

    public static void toRemote(final Context context, final RemoteConnectBean remoteConnectBean) {

        ActivityHttpHelper<BaseResultWrapper<RemoteConnectBean>> helper = new ActivityHttpHelper<>(new TypeToken<BaseResultWrapper<RemoteConnectBean>>() {
        });
        HashMap<String, String> map = new HashMap<>();
        map.put("token",LoginManager.getInstance().getToken());
        map.put("id",remoteConnectBean.id+"");
        helper.UpdateUIDataListener(new IUIDataListener() {
            @Override
            public void uiDataError(Exception exc) {

            }

            @Override
            public void uiDataSuccess(Object obj) {
                BaseResultWrapper<RemoteConnectBean> wrapper = (BaseResultWrapper<RemoteConnectBean>) obj;
                if (wrapper.data!=null) {
                    RemoteConnectBean bean = wrapper.data;
                    String[] split = bean.ip.split(":");
                    ConnectionBean connectionBean = new ConnectionBean(context);
                    connectionBean.setAddress(split[0]);
                    connectionBean.setPort(Integer.parseInt(split[1]));
                    connectionBean.set_Id(remoteConnectBean.id);
                    connectionBean.setToken(bean.token);
                    MyLog.e("打开远程设备 ip : " + bean.ip+"  id = "+connectionBean.get_Id());
                    Intent intent = new Intent(context, RemoteCanvasActivity.class);
                    intent.putExtra(Utils.getConnectionString(context), connectionBean.Gen_getValues());
                    context.startActivity(intent);
                }
            }
        });
        helper.sendPostRequest(HttpConstants.getDeviceConnect,map,HttpConstants.TIME_OUT3);

    }
    public static void toRemote(final int id,final IUIDataListener listener) {

        ActivityHttpHelper<BaseResultWrapper<RemoteConnectBean>> helper = new ActivityHttpHelper<>(new TypeToken<BaseResultWrapper<RemoteConnectBean>>() {
        });
        HashMap<String, String> map = new HashMap<>();
        map.put("token",LoginManager.getInstance().getToken());
        map.put("id",id+"");
        helper.UpdateUIDataListener(listener);
        helper.sendPostRequest(HttpConstants.getDeviceConnect,map,HttpConstants.TIME_OUT3);

    }


    public static void canvasStart(Context context) {
        ActivityManager.MemoryInfo info = Utils.getMemoryInfo(context);
        if (info.lowMemory)
            System.gc();

//        UserInfo userInfo = LoginManager.getInstance().getInfo();
//        List<RemoteConnectBean> remoteConnectBeans = userInfo.remoteConnectBeans;
//        HashMap<Integer, Integer> map = new HashMap<>();
//        for (int i = 1; i <= 60; i++) {
//            int y = 6900 + i;
//            map.put(y, 1);
//        }
//
//        for (int i = 0; i < remoteConnectBeans.size(); i++) {
//            RemoteConnectBean remoteConnectBean = remoteConnectBeans.get(i);
//            int port = remoteConnectBean.port;
//            map.remove(port);
//        }
//        for (int port : map.keySet()) {
//            ConnectionBean connectionBean = new ConnectionBean(context);
//            String arg_address = "192.168.50.193";
//            connectionBean.setAddress(arg_address);
//            connectionBean.setPort(port);
//            RemoteConnectBean remoteConnectBean = new RemoteConnectBean();
//            remoteConnectBean.ip = arg_address;
//            remoteConnectBean.port = port;
//            remoteConnectBeans.add(remoteConnectBean);
//            SharepreferenceUtils.saveClass(App.getContext(), SharepreferenConstants.SHARE_NAME_FILE, SharepreferenConstants.USER_INFO_NODE, userInfo);
//            EventBus.getDefault().post(new bVNCEvent.UInotifyEvent());
////            Intent intent = new Intent(context, RemoteCanvasActivity.class);
////            intent.putExtra(Utils.getConnectionString(context), connectionBean.Gen_getValues());
////            context.startActivity(intent);
////            break;
//        }
    }

}
